package com.wx.mall.operate.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.operate.entity.RebSort;
import com.wx.mall.operate.mapper.RebSortMapper;
import com.wx.mall.operate.service.IRebSortService;
import com.wx.mall.operate.vo.RebSortReqNodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author z
 * @since 2020-6-4
 */
@Service
public class RebSortServiceImpl extends ServiceImpl<RebSortMapper, RebSort> implements IRebSortService {

    @Autowired
    RebSortMapper rebSortMapper;

    @Override
    public List<RebSortReqNodeVo> findsSort() {
        List<RebSort> list = rebSortMapper.selectList(null);
        List<RebSortReqNodeVo> tree=findTree(list);
        return tree;
    }

    private List<RebSortReqNodeVo> findTree(List<RebSort> all){
        List<RebSortReqNodeVo> reqNodeVos= JSON.parseArray(JSON.toJSONString(all),RebSortReqNodeVo.class);
        List<RebSortReqNodeVo> list=new ArrayList<>();
        if (all.isEmpty()){
            return list;
        }
        for (RebSortReqNodeVo rebSortVo:reqNodeVos
             ) {
            if (rebSortVo.getTotalId()==0){
                rebSortVo.setChildren(findChildren(rebSortVo.getId(),all));
                list.add(rebSortVo);
            }
        }
        return list;
    }

    private List<RebSortReqNodeVo> findChildren(Long id,List<RebSort> all){
        List<RebSortReqNodeVo> reqNodeVos = JSON.parseArray(JSON.toJSONString(all), RebSortReqNodeVo.class);
        List<RebSortReqNodeVo> list=new ArrayList<>();
        for (RebSortReqNodeVo rebSortVo:reqNodeVos
             ) {
            if (rebSortVo.getTotalId()==id){
                rebSortVo.setChildren(findChildren(rebSortVo.getId(),all));
                list.add(rebSortVo);
            }
        }
        return list;
    }
}
